package com.ycsj.goldmedalnewconcept.student.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ycsj.common.image.ImageUtil;
import com.ycsj.common.utils.DateUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.student.bean.HyInfo;

import java.util.List;

/**
 * 好友
 * Created by Administrator on 2019/3/1 0001.
 */

public class HyRvAdapter extends BaseQuickAdapter<HyInfo.DataBean, BaseViewHolder> {
    private OnClickBfhy onClickBfhy;
    private Context context;

    public HyRvAdapter(Context context, int layoutResId, @Nullable List<HyInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final HyInfo.DataBean item) {

        //排名的TextView
        TextView tvPm = helper.getView(R.id.tv_pm);
        tvPm.setText((helper.getLayoutPosition() + 1) + "");

        //排名的图标
        ImageView lvPm = helper.getView(R.id.lv_flag);

        if (helper.getLayoutPosition() > 2) {
            tvPm.setVisibility(View.VISIBLE);
            lvPm.setVisibility(View.GONE);
        } else {
            tvPm.setVisibility(View.GONE);
            lvPm.setVisibility(View.VISIBLE);
            if (helper.getLayoutPosition() == 0) {
                lvPm.setImageResource(R.drawable.hy_one);
            } else if (helper.getLayoutPosition() == 1) {
                lvPm.setImageResource(R.drawable.hy_two);
            } else if (helper.getLayoutPosition() == 2) {
                lvPm.setImageResource(R.drawable.hy_three);
            }
        }

        //包含头像,姓名,等级的线性布局
        LinearLayout llOwn = helper.getView(R.id.ll_own);

        //头像
        RoundedImageView lvHead = helper.getView(R.id.lv_head);
        ImageUtil.displayImage(context, lvHead, item.getHeadimg(), R.drawable.head_default);

        //姓名
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getNick_name());

        //等级
        TextView tvLevle = helper.getView(R.id.tv_levle);
        if ("1".equals(item.getHaspet())) {
            tvLevle.setVisibility(View.VISIBLE);
            tvLevle.setText("LV." + item.getLevel());
        } else {
            tvLevle.setVisibility(View.INVISIBLE);
            tvLevle.setText("");
        }


        //跳转到好友宠物家里面
        llOwn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到好友界面
                if (onClickBfhy != null) {
                    onClickBfhy.onclick(item);
                }
            }
        });

        //收取好友的牛奶
        ImageView lvBf = helper.getView(R.id.lv_bf);

        //是否有牛奶可领取的标识
        ImageView lvHasMilk = helper.getView(R.id.lv_gth);

        if ("0".equals(item.getHasmilk())) {
            //没有牛奶可领取额
            lvHasMilk.setVisibility(View.INVISIBLE);
            lvBf.setVisibility(View.INVISIBLE);
        } else {
            lvBf.setVisibility(View.VISIBLE);
            //判断是否快过期,过期则显示,否则不显示
            if (DateUtils.isOverOneDay(item.getEndDate())) {
                lvHasMilk.setVisibility(View.INVISIBLE);
            } else {
                lvHasMilk.setVisibility(View.VISIBLE);
            }
        }

        lvBf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickBfhy != null) {
                    onClickBfhy.onclick(item);
                }
            }
        });

    }


    public void setOnClickBfhy(OnClickBfhy onClickBfhy) {
        this.onClickBfhy = onClickBfhy;
    }

    /**
     * 回调接口
     */
    public interface OnClickBfhy {
        void onclick(HyInfo.DataBean item);
    }
}
